﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace urBook.Engine
{
    /// <summary>
    /// Wrapup Object representation of the Address Book.
    /// </summary>
    [XmlRoot("AddressBook")]
    public class AddressBook : ICloneable
    {
        /// <summary>
        /// Default constructor for Address Book object.
        /// </summary>
        public AddressBook()
        {
            contacts = new List<ContactCard>();
            groups = new List<Group>();
        }
        
        /// <summary>
        /// List of contacts in the Address Book
        /// </summary>
        [XmlArray("Contacts")]
        [XmlArrayItem("ContactCard")]
        public List<ContactCard> contacts { get; set; }

        /// <summary>
        /// List of groups in the Address Book
        /// </summary>
        [XmlArray("Groups")]
        [XmlArrayItem("Group")]
        public List<Group> groups { get; set; }

        /// <summary>
        /// <returns>A string representation for contacts and groups in the Address Book</returns>
        /// </summary>
        public override string ToString()
        {
            string result = "Address Book:" + "\n" +
                            "\t" + "Contacts: " + contacts.Count + "\n" +
                            "\t" + "Groups: " + groups.Count + "\n" + "\n";
            foreach (ContactCard contact in contacts)
                result += contact.ToString() + "\n";
            return result;
        }

        /// <summary>
        /// <returns>A string representation in vCard format for contacts and groups in the Address Book</returns>
        /// </summary>
        public string toVcfString()
        {
            string result = "";
            foreach (ContactCard contact in contacts)
                result += contact.toVcfString() + "\n";
            return result;
        }

        /// <summary>
        /// <returns>Returns a clone for the current object</returns>
        /// <see cref="System.ICloneable"/>
        /// </summary>
        public object Clone()
        {
            AddressBook clone = new AddressBook();
            foreach (ContactCard c in contacts)
                clone.contacts.Add((ContactCard)c.Clone());
            foreach (Group g in groups)
                clone.groups.Add((Group)g.Clone());
            return clone;
        }
    }
}
